import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/model/get_trip_order_list.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/utils/toast.dart';
import 'package:C20Driver/widget/no_data_widget.dart';
import 'package:C20Driver/widget/spinkit.dart';

class IntercityOrder extends StatefulWidget {
  @override
  _IntercityOrderState createState() => _IntercityOrderState();
}

class _IntercityOrderState extends State<IntercityOrder>
    with AutomaticKeepAliveClientMixin {
  ScrollController _scrollController = new ScrollController();

  @override
  bool get wantKeepAlive => true;

  List _list;
  int _page = 1;

  @override
  void initState() {
    super.initState();
    _getItineraryOrder();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _getItineraryOrderPullUpLoading();
      }
    });
  }

  /// 获取行程订单列表 分为历史行程订单和行程中订单
  Future _getItineraryOrder() async {
    _list =
        await Request.getItineraryOrder(page: _page.toString(), type: '0030');
    if (mounted) {
      setState(() {
        _list = _list;
      });
    }
  }

  /// 上拉加载我的行程列表
  Future _getItineraryOrderPullUpLoading() async {
    List _value = await Request.getItineraryOrder(
        page: (_page + 1).toString(), type: '0030');
    if (mounted) {
      if (_value.length != 0) {
        setState(() {
          _page++;
          _list.addAll(_value);
        });
      } else {
        ToastX.center('没有更多数据');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return _list == null
        ? Spinkit()
        : Container(
            padding: EdgeInsets.only(
              left: ScreenUtil().setWidth(32),
              right: ScreenUtil().setWidth(32),
            ),
            width: double.infinity,
            height: double.infinity,
            color: Color(0xffEFF3F9),
            alignment: Alignment.center,
            child: _list.length != 0
                ? ListView.builder(
                    controller: _scrollController,
                    itemCount: _list == null ? 0 : _list.length,
                    itemBuilder: (context, index) {
                      var item = (_list)[index];
                      return _itineraryOrderCard(item);
                    },
                  )
                : NoDataWidget(),
          );
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  // 行程订单卡片
  Widget _itineraryOrderCard(val) {
    var getTripOrderListData = GetTripOrderList.fromJson(val);
    String statusStr = '';
    if ('0000' == getTripOrderListData.status) {
      statusStr = '订单完成';
    } else if ('0060' == getTripOrderListData.status) {
      statusStr = '已取消';
    } else {
      statusStr = '进行中';
    }
    return Container(
      margin: EdgeInsets.only(top: ScreenUtil().setWidth(32)),
      padding: EdgeInsets.all(ScreenUtil().setWidth(32)),
      decoration: BoxDecoration(
        color: Color(0xffffffff),
        borderRadius: BorderRadius.all(
          Radius.circular(
            ScreenUtil().setWidth(32),
          ),
        ),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '顺风车订单',
                style: TextStyle(
                    color: Color(0xff333333),
                    fontSize: ScreenUtil().setWidth(30),
                    fontWeight: FontWeight.w500),
              ),
              Text(
                statusStr,
                style: TextStyle(
                    color: statusStr == '进行中'
                        ? Color(0xff3FB956)
                        : Color(0xff333333),
                    fontSize: ScreenUtil().setWidth(28),
                    fontWeight: FontWeight.w400),
              ),
            ],
          ),
          SizedBox(
            height: ScreenUtil().setWidth(32),
          ),
          Text(
            getTripOrderListData.depTime != null
                ? getTripOrderListData.depTime
                : '',
            style: TextStyle(
              fontSize: ScreenUtil().setSp(30.0),
              color: Color.fromARGB(255, 102, 102, 102),
            ),
          ),
          SizedBox(
            height: ScreenUtil().setWidth(28),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Container(
                width: getTripOrderListData.price.toString().length >= 6
                    ? ScreenUtil().setWidth(460)
                    : ScreenUtil().setWidth(500),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(
                            right: ScreenUtil().setWidth(30),
                          ),
                          width: ScreenUtil().setWidth(15.0),
                          height: ScreenUtil().setWidth(15.0),
                          decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            color: Color(0xff00BA8F),
                          ),
                        ),
                        Expanded(
                          child: Container(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              getTripOrderListData.depAre,
                              style: TextStyle(
                                fontSize: ScreenUtil().setSp(30.0),
                                color: Color(0xff333333),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: ScreenUtil().setWidth(28.0)),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(
                            left: ScreenUtil().setWidth(0),
                            right: ScreenUtil().setWidth(30),
                          ),
                          width: ScreenUtil().setWidth(15.0),
                          height: ScreenUtil().setWidth(15.0),
                          decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            color: Color.fromARGB(255, 255, 102, 0),
                          ),
                        ),
                        Expanded(
                          child: Container(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              getTripOrderListData.destArea,
                              style: TextStyle(
                                fontSize: ScreenUtil().setSp(30.0),
                                color: Color(0xff333333),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              RichText(
                text: TextSpan(
                  text: '￥',
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(28.0),
                    color: Color(0xff333333),
                  ),
                  children: <TextSpan>[
                    TextSpan(
                      text:
                          '${getTripOrderListData.price.toString() != null ? getTripOrderListData.price.toString() : "0.0"}',
                      // '123.88',
                      style: TextStyle(
                          fontSize: ScreenUtil().setSp(36.0),
                          color: Color(0xff333333),
                          fontWeight: FontWeight.w500),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
